<?php

class importexport_controller extends desktop_controller {

    public function __construct($app) {
        parent::__construct($app);
    }

    public function export_support_filetype() {
        $filetype = array(
            'csv' => '.csv',
            'xls' => '.xls'
        );
        return $filetype;
    }

    public function import_support_filetype() {
        $filetype = array(
            'csv' => '.csv',
            'xls' => '.xls'
        );
        return $filetype;
    }

    public function storage_policy() {
        return kernel::single('importexport_policy')->storage_policy();
    }

    public function set_storage_params($params) {
        return kernel::single('importexport_policy')->set_storage_params($params);
    }

    public function get_storage_params() {
        return kernel::single('importexport_policy')->get_storage_params();
    }

    public function check_policy() {
        if (! $this->get_storage_params()) {
            return false;
        }
        return true;
    }

    public function queue_policy() {
        $server = $this->storage_policy();
        return $server['policy'];
    }

    public function gen_key($type = 'export') {
        $key = $type . '-' . time();
        return $key;
    }

    public function view_filter($filter, $params) {
        $ctl_class = $filter['app'] . '_ctl_' . $filter['ctl'];
        // $mdl_class = $params['app'].'_mdl_'.$params['mdl'];
        $mdl_class = $params['mdl'];
        if (! isset($filter['view'])) {
            $_POST['view'] = $filter['_finder']['in_pager'] - 1;
        } else {
            $_POST['view'] = $filter['view'];
        }
        
        $view_filter = $this->get_view_filter($ctl_class, $mdl_class);
        
        $filter = array_merge($filter, $view_filter);
        return $filter;
    }

    public function import_message($status = false, $msg) {
        if ($status) {
            $status_msg = app::get('importexport')->_('上传成功');
        } else {
            $status_msg = app::get('importexport')->_('上传失败');
        }
        header("content-type:text/html; charset=utf-8");
        echo "<script>top.MessageBox.success(\"" . $status_msg . "\");alert(\"" . $msg . "\");if(parent.$('import_form').getParent('.dialog'))parent.$('import_form').getParent('.dialog').retrieve('instance').close();if(parent.window.finderGroup&&parent.window.finderGroup['" . $_GET['finder_id'] . "'])parent.window.finderGroup['" . $_GET['finder_id'] . "'].refresh();</script>";
        exit();
    }

    // 文件下载
    public function file_download(&$msg) {
        $params = app::get('importexport')->model('task')->getList('*', array(
            'task_id' => $_GET['task_id']
        ));
        $params = $params[0];
        
        if ($params['status'] == '2' || $params['status'] == '6' || $params['status'] == '8') {
            // 连接存储服务器
            $this->policyObj = kernel::single('importexport_policy');
            $msg = $this->policyObj->connect();
            if ($msg !== true) {
                return false;
            }
            
            $filename = $this->policyObj->create_remote_file($params);
            if (! $this->policyObj->create_local_file()) {
                $msg = app::get('importexport')->_('本地文件创建失败，请检查/tmp文件夹权限');
                return false;
            }
            
            $filetypeObj = kernel::single('importexport_type_' . $params['filetype']);
            
            $size = $this->policyObj->remote_file_size($filename);
            if ($size === 0) {
                $msg = app::get('importexport')->_('需要下载的文件为空，不需要下载');
                return false;
            }
            if ($size < 0) {
                $msg = app::get('importexport')->_('需要下载的文件在ftp端不存在，请检查ftp服务器和原始数据');
                return false;
            }
            
            $range = $filetypeObj->set_queue_header($filename, $size);
            if (! $this->policyObj->pull(array(
                'resume' => $range
            ), $msg)) {
                return false;
            }
            
            // 实例化导出文件类型类
            $file = fopen($this->policyObj->local_file, "rb");
            if (method_exists($filetypeObj, 'setBom')) {
                $bom = $filetypeObj->setBom();
                echo $bom;
            }
            while (! feof($file)) {
                set_time_limit(0);
                print_r(fread($file, 1024 * 8));
                ob_flush();
                flush();
            }
            $this->policyObj->close_local_file();
        }
        exit();
    }
}
